package com.bs.bszhsq.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.bs.bszhsq.entity.Role;
import com.bs.bszhsq.entity.RoleMenu;
import com.bs.bszhsq.mapper.RoleMapper;
import com.bs.bszhsq.mapper.RoleMenuMapper;
import com.bs.bszhsq.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 章宇光
 * @since 2024-03-30
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {
    @Resource
    private RoleMenuMapper roleMenuMapper;

    @Override
    @Transactional
    public void setRoleMenu(Integer roleId, List<Integer> menuId) {
        LambdaQueryWrapper<RoleMenu> lqw=new LambdaQueryWrapper<>();
        lqw.eq(roleId!=null,RoleMenu::getRoleId,roleId);
        int delete = roleMenuMapper.delete(lqw);
        RoleMenu roleMenu=new RoleMenu();
       roleMenu.setRoleId(roleId);
        for (Integer integer : menuId) {
            roleMenu.setMenuId(integer);
            roleMenuMapper.insert(roleMenu);
        }
    }
}
